package com.FH.organization.noticeform_member;

import com.FH.applyFrom.notic_form.NoticeFormResource;
import com.FH.applyFrom.notic_form.NoticeFormShowDTO;
import com.FH.organization.remind.RemindPullDTO;
import com.FH.organization.remind.RemindResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/11/2.
 */
@Stateless
public class NoticeFormMemberDAOImpl extends AccessDB implements NoticeFormMemberDAO{


    @Inject
    NoticeFormMemberResource noticeFormMemberResource;

    @Inject
    RemindResource remindResource;

    @Inject
    NoticeFormResource noticeFormResource;

    /**
     * 查询发文提醒人员
     * @param search
     * @param size
     * @param page
     * @return
     */
    @Override
    public ResultDTO getNoticeFormMemberInfo(String search, String size, String page) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from noticeform_member where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if(json.has("id")){
                sql_w += " AND id = '"+json.getString("id")+"'";
            }
            if(json.has("member_id")){
                sql_w += " AND member_id = '"+json.getString("member_id")+"'";
            }


            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String []> seresult = new ArrayList<String []>();

            List<NoticeFormMemberShowDTO> result = new ArrayList<NoticeFormMemberShowDTO>();

            if(!existName.equals("0")){

                String sql ="select id,member_name,member_id,notice_form_id from  " +
                        "noticeform_member where 1=1" +sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    //   NoticeFormShowDTO nfs = new NoticeFormShowDTO();
                    NoticeFormMemberShowDTO nfs = new NoticeFormMemberShowDTO();
                    nfs.setId(seresult.get(i)[1]);
                    nfs.setMember_id(seresult.get(i)[2]);
                    nfs.setMember_name(seresult.get(i)[3]);
                    nfs.setNotice_form_id(seresult.get(i)[4]);
                    nfs.setMember_id(seresult.get(i)[5]);


                    result.add(nfs);

                }
            }

            if(seresult.size()!=0) {
                resultDTO.setStr(result.size()+"");
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);//"发文提醒人员调用成功"
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }

    /**
     * 添加发文提醒人员
     * @param noticeFormMemberPullDTO
     * @return
     */
    @Override
    public ResultDTO addNoticeFormMembersInfo(NoticeFormMemberPullDTO noticeFormMemberPullDTO) {

        ResultDTO resultDTO = new ResultDTO();
        /**
         * JSON解析集合
         */
        if (null != noticeFormMemberPullDTO.getNoticeformmember() && !"".equals(noticeFormMemberPullDTO.getNoticeformmember())) {
            String noticeformmember = noticeFormMemberPullDTO.getNoticeformmember();

            try {
                JSONArray array = new JSONArray(noticeformmember);
                if (array != null && array.length() > 0) {
                    List<NoticeFormMemberPullDTO> result = new ArrayList<NoticeFormMemberPullDTO>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        String ids = jsonObject.getString("id");
                        String name = jsonObject.getString("name");
                        NoticeFormMemberPullDTO noticeFormMemberPullDTO1 = new NoticeFormMemberPullDTO();
                        noticeFormMemberPullDTO1.setNotice_form_id(noticeFormMemberPullDTO.getNotice_form_id());
                        noticeFormMemberPullDTO1.setMember_id(ids);
                        noticeFormMemberPullDTO1.setMember_name(name);
                        int resultls = 0;

                        String existName = "";
                        /**
                         * 判断报销申请单中是否已存在此数据
                         *
                         * 若存在  进行更新
                         *
                         * 如不存在 执行add方法
                         *
                         */

                        //  noticeFormMemberResource.addNoticeFormMembersInfo(noticeFormMemberPullDTO);
                        String id = noticeFormMemberPullDTO.getId();
                        if (id != null && !id.equals("")) {

                            String sql = "select count(1) from noticeform_member where id='" + noticeFormMemberPullDTO.getId() + "'";

                            existName = getReportingToOne(sql);

                            if (!existName.equals("0")) {
                                return noticeFormMemberResource.updateNoticeFormMemberInfo(noticeFormMemberPullDTO);
                            }
                        }

                        String Expen_Form_insert = "";
                        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String time = df.format(new Date());
                        Expen_Form_insert += "insert into noticeform_member(id,member_name,member_id,notice_form_id) VALUES('" + uuid + "'," +
                                "'" + name + "','" + ids + "','" + noticeFormMemberPullDTO.getNotice_form_id() + "')";
                        //插入业务表
                        resultls = addReportingTo(Expen_Form_insert);
                        if (resultls != 0) {
                            resultDTO.setStr(uuid);
                            resultDTO.setStatus("90000");
                            resultDTO.setDescritpion(warnSrc.add_success);

                            try {
                                ResultDTO list = null;
                                list = noticeFormResource.getNoticeFormInfo("{notice_form_id:"+noticeFormMemberPullDTO.getNotice_form_id()+ ",isverify:" + 1 + "}", "1", "20");
                                ArrayList<NoticeFormShowDTO>  mens=(ArrayList<NoticeFormShowDTO>)list.getValues();// List集合  强转

                                for(int y = 0;y<mens.size();y++) {
                                    NoticeFormShowDTO noticeFormShowDTO = mens.get(y);

                                    Calendar   cal   =   Calendar.getInstance();
                                    cal.add(Calendar.DATE,   +7);
                                    String yesterday = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(cal.getTime());
                                    System.out.println(yesterday);

                                    RemindPullDTO remindPullDTO = new RemindPullDTO();
                                    remindPullDTO.setRemind_content(noticeFormShowDTO.getNotice_form_title());
                                    remindPullDTO.setRemind_time(time);
                                    remindPullDTO.setRemind_type("10");
                                    remindPullDTO.setRemind_form_id(noticeFormMemberPullDTO.getNotice_form_id());
                                    remindPullDTO.setRemind_order("0");
                                    remindPullDTO.setCreate_time(time);
                                    remindPullDTO.setRemind_endtime(yesterday);
                                    remindPullDTO.setRemind_member_id(ids);
                                    remindPullDTO.setRemind_starttime("");
                                    remindPullDTO.setRemind_descritpion("您有一条新的发文消息，请注意查收！");

                                    remindResource.addRemindInfo(remindPullDTO);


                                }

                        } catch (Exception e) {
                            e.printStackTrace();

                    }
                        } else {
                            resultDTO.setStatus("90001");
                            resultDTO.setDescritpion(warnSrc.add_fail);
                        }

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }


        }

            return resultDTO;
        }

        /**
         * 修改发文提醒人员
         * @param noticeFormMemberPullDTO
         * @return
         */
        @Override
        public ResultDTO updateNoticeFormMemberInfo (NoticeFormMemberPullDTO noticeFormMemberPullDTO){
            return null;
        }

        /**
         * 删除发文提醒人员
         * @param noticeFormMemberPullDTO
         * @return
         */
        @Override
        public ResultDTO deleteNoticeFormMemberInfo (NoticeFormMemberPullDTO noticeFormMemberPullDTO){
            //删除之前查看该表是否有记录
            String sql = "select count(1) from noticeform_member where notice_form_id='" + noticeFormMemberPullDTO.getNotice_form_id() + "'";
            String existName = getReportingToOne(sql);

            int result = 0;

            ResultDTO resultDTO = new ResultDTO();

            if (existName != null) {
                String QUERY = "";
                QUERY += "delete from noticeform_member where notice_form_id='" + noticeFormMemberPullDTO.getNotice_form_id() + "'";

                result = this.addReportingTo(QUERY);
            }

            if (result != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.update_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.update_fail);

            }
            return resultDTO;
        }

    }